﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using MVC.Models;
using MVC.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MVC.Controllers {
  public class HomeController : Controller {
    private readonly IStudentRepository _studentRepository;
    private readonly IWebHostEnvironment _environment;

    public HomeController(IStudentRepository studentRepository, IWebHostEnvironment environment) {
      _studentRepository = studentRepository;
      _environment = environment;
    }
    public IActionResult Index() {
      IEnumerable<Student> students = _studentRepository.GetStudents();
      return View(students);
    }
    public IActionResult Detail(int id) {
      Student student = _studentRepository.GetStudent(id);
      HomeDetailViewModel homeDetailViewModel = new HomeDetailViewModel { Student = student, Title = "Detail" };
      return View(homeDetailViewModel);
    }
     
    public IActionResult Create() {
      return View();
    }
    [HttpPost]
    public IActionResult Create(CreateViewModel student) {
      if (ModelState.IsValid) {
        string uniqueFileName = UploadAvatar(student);
        Student newStudent = new Student {
          Name = student.Name,
          Email = student.Email,
          ClassName = student.ClassName,
          AvatarPath = uniqueFileName
        };
        _studentRepository.AddStudent(newStudent);
        return RedirectToAction("detail", new { id = newStudent.Id });
      }
      return View();

    }
    public IActionResult Edit(int id) {
      Student student = _studentRepository.GetStudent(id);
      EditViewModel editViewModel = new EditViewModel() {
        Id = student.Id,
        Name = student.Name,
        Email = student.Email,
        ClassName = student.ClassName,
        EditAvatarPath=student.AvatarPath
      };
      return View(editViewModel);

    }
    [HttpPost]
    public IActionResult Edit(EditViewModel model) {
      string uniqueFileName = null;
      if (ModelState.IsValid) {
        if (model.Avatar != null && model.Avatar.Count >0) {
          if (model.EditAvatarPath != null) {
            string filePath = Path.Combine(_environment.WebRootPath, "images", model.EditAvatarPath);
              System.IO.File.Delete(filePath);
            }          
          uniqueFileName = UploadAvatar(model);
        }
        Student student = _studentRepository.GetStudent(model.Id);
        student.Name = model.Name;
        student.Email = model.Email;
        student.ClassName = model.ClassName;
        student.AvatarPath = uniqueFileName;
        Student updateStudent=_studentRepository.UpdateStudent(student);
        return RedirectToAction("detail", new { id = student.Id });
      }
      return View();
    }
    private string UploadAvatar(CreateViewModel model) {
      string uniqueFileName = null;
      if (model.Avatar != null && model.Avatar.Count >0) {
        foreach (var i in model.Avatar) {
          string uploadsFolder = Path.Combine(_environment.WebRootPath, "images");
          uniqueFileName = Guid.NewGuid().ToString() + "_" + i.FileName;
          string filePath = Path.Combine(uploadsFolder, uniqueFileName);
          using (var stream = new FileStream(filePath, FileMode.Create)) {
            i.CopyTo(stream);
          } 
        }
      }
      return uniqueFileName;
    }

  }
}
